/*
//  Copyright © 2014 Allen County Public Library.

  //        ACPL Mobile is free software: This program is distributed and you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.  ACPL Mobile is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY, including without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with ACPL Mobile. If not, see http://www.gnu.org/licenses/.
 * Author: James Harpe
 * 
 * 
 * Description: This activity is responsible for displaying detailed information about a particular event.
 * 				Event information is passed to this activity using the intent object. It also contains methods
 * 				for maintaining a list of remembered events for the user.
 */

package us.in.lib.acpl.Mobile_ACPL;

import java.util.ArrayList;
import java.util.List;

import com.google.analytics.tracking.android.EasyTracker;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.TextView;
import us.in.lib.acpl.Mobile_ACPL.context.Mobile_ACPL;
import us.in.lib.acpl.Mobile_ACPL.parents.BaseActivity;
import us.in.lib.acpl.Mobile_ACPL.util.JSONArrayPrefs;

public class EventDetailActivity extends BaseActivity implements
		OnClickListener {
	private SharedPreferences prefs;
	private SharedPreferences.Editor editor;
	private String id;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		this.getActionBar().setTitle("  Event Details");
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.event_detail);

		prefs = this.getSharedPreferences("myPrefs",
				Context.MODE_WORLD_WRITEABLE);
		editor = prefs.edit();
		id = getIntent().getStringExtra("id");
		displayEventInfo();
	}
	  @Override
	  public void onStart() {
	    super.onStart();
	    EasyTracker.getInstance().activityStart(this); // Add this method.
	  }

	  @Override
	  public void onStop() {
	    super.onStop();
	    EasyTracker.getInstance().activityStop(this); // Add this method.
	  }
	public void displayEventInfo() {
		Intent intent = getIntent();
		TextView t = (TextView) findViewById(R.id.eventTitle);
		t.setText(intent.getStringExtra("title"));

		t = (TextView) findViewById(R.id.eventDetailDate);
		t.setText(intent.getStringExtra("date"));

		t = (TextView) findViewById(R.id.eventDetailTime);
		t.setText(intent.getStringExtra("time"));

		Spanned desc = Html.fromHtml(intent.getStringExtra("description"));
		t = (TextView) findViewById(R.id.description);
		t.setText(desc.toString());

		String library = intent.getStringExtra("library");
		t = (TextView) findViewById(R.id.library);
		t.setText(library);
		
		String ageGroups = intent.getStringExtra("ageGroups");
		t = (TextView) findViewById(R.id.agegroups);
		t.setText(ageGroups);
		if (ageGroups!=null&&!ageGroups.equals("")){
			t.setVisibility(View.VISIBLE);
		}

		String location = intent.getStringExtra("location");
		if (!library.equalsIgnoreCase(location)) {
			t = (TextView) findViewById(R.id.location);
			t.setText(location);
			t.setVisibility(View.VISIBLE);
		}
		if (!intent.getStringExtra("length").equalsIgnoreCase("0")) {

			t = (TextView) findViewById(R.id.length);
			t.setVisibility(View.VISIBLE);
			t.setText(intent.getStringExtra("length") + " minutes");
		}

		
		CheckBox c = (CheckBox) findViewById(R.id.eventReminderBox);
		c.setOnClickListener(this);
	
			
			List<String> eventList = JSONArrayPrefs.getStringArrayPref(this,
					"events");

			String id = getIntent().getStringExtra("id");
			if (eventList.contains(id)) {

				c.setChecked(true);
			}

		

	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == Activity.RESULT_OK) {
			CheckBox c = (CheckBox) findViewById(R.id.eventReminderBox);
			String user = prefs.getString("user", null);
			ArrayList<String> events = JSONArrayPrefs.getStringArrayPref(this,
					user + "Events");
			if (c.isChecked()) {
				events.add(id);
				JSONArrayPrefs
						.setStringArrayPref(this, user + "Events", events);
			} else {
				events.remove(id);
				JSONArrayPrefs
						.setStringArrayPref(this, user + "Events", events);
			}
		}
	}

	@Override
	public void onClick(View view) {

			CheckBox c = (CheckBox) view;

			ArrayList<String> events = JSONArrayPrefs.getStringArrayPref(this,
					"events");
			if (c.isChecked()) {
				events.add(id);
				JSONArrayPrefs
						.setStringArrayPref(this, "events", events);

				

				EasyTracker.getTracker().sendEvent("Events", "Event Reminder Box Checked", null, null);

			} else {
				events.remove(id);
				JSONArrayPrefs
						.setStringArrayPref(this, "events", events);
				

				EasyTracker.getTracker().sendEvent("Events", "Event Reminder Box Unchecked", null, null);
			}
		

	}


}
